package com.chyer.logistics_interest.entity;

import lombok.extern.slf4j.Slf4j;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.protocol.HTTP;
import org.apache.http.util.EntityUtils;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.*;

@Slf4j
public class Book1984Craw {


    public static void main(String[] args) throws IOException {
        long start = System.currentTimeMillis();

        long end = System.currentTimeMillis();
        log.info("start = {}, end={},耗时={}", start, end, (end - start));
    }

    public static String doPost(Integer pageNum, String token, String t, String year) {
        String result = null;
        try {
            HttpClient httpClient = HttpClients.createDefault();
            HttpPost request = new HttpPost("http://222.143.21.233:2018/21To23/pub/GongShiSearch"); //对方的url

            request.setHeader("Content-Type", "application/x-www-form-urlencoded;charset=utf-8");
            request.setHeader("Cookie", "td_cookie=2112575280; ASP.NET_SessionId=uhot0lq2mj1y35jyzuqxjrjh; __RequestVerificationToken_LzIxVG8yMw__=xiWob5DvzNqLl5jFJaClzsXDxGJi6zIWSHrX7XTkQzc88ZGbudCIF30x/CNIHn7V1IDDL7lYgZVsR2/Lwk60P3lU8Y0A2BJTwLrej9UWwV4Z31a2LG65QaQJTTgqXp0+jl+ueJ1O8nT69hbXl4VrZAn7EcA7wLTx6w0IRt6jW/I=");

            Map<String, String> formParams = new HashMap<String, String>();
            //验证token 和上面的 Cookie 保持一致
            formParams.put("__RequestVerificationToken", token);
            formParams.put("JiJuLeiXing", "轮式拖拉机");
            formParams.put("JiJuLeiXingCode", "200101");
            formParams.put("YearNum", year);
            if(pageNum == 0){
                formParams.put("t", "10c216b8ec0e4364807790582cadbc3e");
            }else{
                formParams.put("t", t);
            }

            //页数
            if(pageNum != null && pageNum > 0){
                formParams.put("PageIndex",pageNum.toString());
            }

            List<NameValuePair> nvps = new ArrayList<NameValuePair>();

            if (formParams != null) {
                Set<String> keySet = formParams.keySet();
                for (String key : keySet) {
                    nvps.add(new BasicNameValuePair(key, formParams.get(key)));
                }
            }

            request.setEntity(new UrlEncodedFormEntity(nvps, HTTP.UTF_8));
            HttpResponse response = httpClient.execute(request);

            if (response.getStatusLine().getStatusCode() == 200) {
                HttpEntity entity = response.getEntity();
                result = EntityUtils.toString(entity, "utf-8");

            }
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        } catch (ClientProtocolException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

        return result;
    }

}
